/**
 * 
 */
package edu.jhu.tcj.purfuit.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.jhu.tcj.purfuit.Category;

/**
 * @author Bobby
 *
 */
public class ManageCatView extends JPanel implements ActionListener{
	
	private JPanel mainPanel;
	private JTextField cat1text;
	private JTextField cat2text;
	private JTextField cat3text;
	private JTextField cat4text;
	private JButton submitButton;
	private JButton cancelButton;
	private JButton colorButton1;
	private JButton colorButton2;
	private JButton colorButton3;
	private JButton colorButton4;
	private JColorChooser colorChooser;
	private JButton colorDone;
	private JButton colorCancel;
	private JPanel cp1;
	private JPanel cp2;
	private JPanel cp3;
	private JPanel cp4;
	
	private JFrame frame;
	
	private Category[] categories;
	
	private JPanel colorChooserPanel;
	private JFrame colorFrame;
	
	private int colorToChange = 1;
	
	public ManageCatView(Category[] categories){
		this.categories = categories;
		
		mainPanel = new JPanel(new GridLayout(0,1));
		cat1text = new JTextField(30);
		cat2text = new JTextField(30);
		cat3text = new JTextField(30);
		cat4text = new JTextField(30);
		
		submitButton = new JButton("Save");
		submitButton.addActionListener(this);
		submitButton.setActionCommand("save");
		
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		cancelButton.setActionCommand("cancel");
		
		colorButton1 = new JButton("Change Color");
		colorButton1.setActionCommand("color1");
		colorButton1.addActionListener(this);
		
		colorButton2 = new JButton("Change Color");
		colorButton2.setActionCommand("color2");
		colorButton2.addActionListener(this);
		
		colorButton3 = new JButton("Change Color");
		colorButton3.setActionCommand("color3");
		colorButton3.addActionListener(this);
		
		colorButton4 = new JButton("Change Color");
		colorButton4.setActionCommand("color4");
		colorButton4.addActionListener(this);
		
		colorDone = new JButton("Update");
		colorDone.addActionListener(this);
		colorDone.setActionCommand("colorupdate");
		
		colorCancel = new JButton("Cancel");
		colorCancel.addActionListener(this);
		colorCancel.setActionCommand("colorcancel");
		
		colorChooser = new JColorChooser();
		colorChooserPanel = new JPanel(new BorderLayout());
		colorChooserPanel.add(colorChooser, BorderLayout.CENTER);
		JPanel colorButtonPanel = new JPanel();
		colorButtonPanel.add(colorDone);
		colorButtonPanel.add(colorCancel);
		colorChooser.add(colorButtonPanel, BorderLayout.SOUTH);
		colorFrame = new JFrame();
		
		JLabel topLabel = new JLabel("Manage Categories");
		topLabel.setFont(new Font(null, Font.BOLD, 20));
		topLabel.setAlignmentX(LEFT_ALIGNMENT);
		mainPanel.add(topLabel);
		
		//this.add(new JSeparator(JSeparator.HORIZONTAL));
		
		JPanel labelFormat = new JPanel(new BorderLayout());
		JLabel cat1label = new JLabel("Category 1");
		cat1label.setBackground(Color.LIGHT_GRAY);
		cat1label.setOpaque(true);
		labelFormat.add(cat1label, BorderLayout.SOUTH);
		mainPanel.add(labelFormat);
		JPanel cat1panel = new JPanel();
		cat1panel.add(new JLabel("Name: "));
		cat1text.setText(categories[0].getName());
		cat1panel.add(cat1text);
		mainPanel.add(cat1panel);
	
		JPanel colorPanel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		cp1 = new JPanel();
		cp1.setPreferredSize(new Dimension(30,30));
		cp1.setBorder(BorderFactory.createLineBorder(Color.black));
		cp1.setBackground(categories[0].getColor());
		colorPanel1.add(cp1);
		colorPanel1.add(colorButton1);
		mainPanel.add(colorPanel1);
		
		JPanel labelFormat2 = new JPanel(new BorderLayout());
		JLabel cat2label = new JLabel("Category 2");
		cat2label.setBackground(Color.LIGHT_GRAY);
		cat2label.setOpaque(true);
		labelFormat2.add(cat2label, BorderLayout.SOUTH);
		mainPanel.add(labelFormat2);
		
		JPanel cat2panel = new JPanel();
		cat2panel.add(new JLabel("Name: "));
		cat2text.setText(categories[1].getName());
		cat2panel.add(cat2text);
		mainPanel.add(cat2panel);
		
		
		JPanel colorPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		cp2 = new JPanel();
		cp2.setBackground(categories[1].getColor());
		cp2.setBorder(BorderFactory.createLineBorder(Color.black));
		cp2.setPreferredSize(new Dimension(30,30));
		colorPanel2.add(cp2);
		colorPanel2.add(colorButton2);
		mainPanel.add(colorPanel2);
		
		JPanel labelFormat3 = new JPanel(new BorderLayout());
		JLabel cat3label = new JLabel("Category 3");
		cat3label.setBackground(Color.LIGHT_GRAY);
		cat3label.setOpaque(true);
		labelFormat3.add(cat3label, BorderLayout.SOUTH);
		mainPanel.add(labelFormat3);
		
		JPanel cat3panel = new JPanel();
		cat3panel.add(new JLabel("Name: "));
		cat3text.setText(categories[2].getName());
		cat3panel.add(cat3text);
		mainPanel.add(cat3panel);
		
		JPanel colorPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		cp3 = new JPanel();
		cp3.setBackground(categories[2].getColor());
		cp3.setBorder(BorderFactory.createLineBorder(Color.black));
		cp3.setPreferredSize(new Dimension(30,30));
		colorPanel3.add(cp3);
		colorPanel3.add(colorButton3);
		mainPanel.add(colorPanel3);
		
		JPanel labelFormat4 = new JPanel(new BorderLayout());
		JLabel cat4label = new JLabel("Category 4");
		cat4label.setBackground(Color.LIGHT_GRAY);
		cat4label.setOpaque(true);
		labelFormat4.add(cat4label, BorderLayout.SOUTH);
		mainPanel.add(labelFormat4);
		
		JPanel cat4panel = new JPanel();
		cat4panel.add(new JLabel("Name: "));
		cat4text.setText(categories[3].getName());
		cat4panel.add(cat4text);
		mainPanel.add(cat4panel);
		
		JPanel colorPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		cp4 = new JPanel();
		cp4.setBackground(categories[3].getColor());
		cp4.setBorder(BorderFactory.createLineBorder(Color.black));
		cp4.setPreferredSize(new Dimension(30,30));
		colorPanel4.add(cp4);
		colorPanel4.add(colorButton4);
		mainPanel.add(colorPanel4);
		mainPanel.add(new JPanel());
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(submitButton);
		buttonPanel.add(cancelButton);
		mainPanel.add(buttonPanel);
		
		this.add(mainPanel);
		frame = new JFrame();
		frame.setContentPane((mainPanel));
		frame.pack();
		frame.setTitle("Trivial Purfuit - Manage Categories");
		frame.setVisible(true);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/**Category cat1 = new Category();
		cat1.setName("People");
		cat1.setColor(new Color(255,255,255));
		Category cat2 = new Category();
		cat2.setName("Places");
		cat2.setColor(new Color(0,0,255));
		Category cat3 = new Category();
		cat3.setName("Events");
		cat3.setColor(new Color(0,255,0));
		Category cat4 = new Category();
		cat4.setName("Holiday");
		cat4.setColor(new Color(255,0,0));
		
		Category[] cats = new Category[4];
		cats[0] = cat1;
		cats[1] = cat2;
		cats[2] = cat3;
		cats[3] = cat4;
		**/
		Category[] cats = null;
		try{
			cats = Category.loadCategories();
		}catch(Exception e){
			e.printStackTrace();
		}
		ManageCatView view = new ManageCatView(cats);
		
		
		JFrame frame = new JFrame();
		frame.setTitle("Trivial Purfuit - Manage Categories");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(view);
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("color1")){
			colorToChange = 1;
			colorFrame.add(colorChooserPanel);
			colorFrame.pack();
			colorFrame.setVisible(true);
		}
		if(e.getActionCommand().equals("color2")){
			colorToChange = 2;
			colorFrame.add(colorChooserPanel);
			colorFrame.pack();
			colorFrame.setVisible(true);
		}
		if(e.getActionCommand().equals("color3")){
			colorToChange = 3;
			colorFrame.add(colorChooserPanel);
			colorFrame.pack();
			colorFrame.setVisible(true);
		}
		if(e.getActionCommand().equals("color4")){
			colorToChange = 4;
			colorFrame.add(colorChooserPanel);
			colorFrame.pack();
			colorFrame.setVisible(true);
		}
		
		if(e.getActionCommand().equals("colorcancel")){
			colorFrame.setVisible(false);
		}
		
		if(e.getActionCommand().equals("colorupdate")){
			Color newColor = colorChooser.getColor();
			if(colorToChange == 1){
				categories[0].setColor(newColor);
				cp1.setBackground(newColor);
			}
			if(colorToChange == 2){
				categories[1].setColor(newColor);
				cp2.setBackground(newColor);
			}
			if(colorToChange == 3){
				categories[2].setColor(newColor);
				cp3.setBackground(newColor);
			}
			if(colorToChange == 4){
				categories[3].setColor(newColor);
				cp4.setBackground(newColor);
			}
			colorFrame.setVisible(false);
		}
		
		if(e.getActionCommand().equals("save")){
			getNewCategories();
			try{
				Category.saveCategories(categories);
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
			frame.setVisible(false);
		}
		
		if(e.getActionCommand().equals("cancel")){
			frame.setVisible(false);
		}
		
	}
	
	public Category[] getNewCategories(){
		categories[0].setName(cat1text.getText());
		categories[1].setName(cat2text.getText());
		categories[2].setName(cat3text.getText());
		categories[3].setName(cat4text.getText());
		return categories;
	}

}
